/* Copyright (c) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.orkut.client.api.samples.greetings.server.servlets;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.orkut.client.api.samples.greetings.server.CachedConfig;
import com.google.orkut.client.api.samples.greetings.server.db.ConfigEntry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Provides a minimalistic UI to configure the site's authentication.
 *
 * @author Shishir Birmiwal
 */
@Singleton
public class AdminServlet extends HttpServlet {
  String htmlPage = "<html><body>"
      + "<form method=POST>"
      + "Consumer Key: <input name=key value=\"KEY\" /><br/>"
      + "Consumer Secret: <input name=secret value=\"SECRET\" /><br/>"
      + "Shared album userid: <input name=shared value=\"SHARED\" /><br/>"
      + "Site name: <input name=sitename value=\"SITENAME\" /><br/>"
      + "Posttoken Salt: <input name=salt value=\"SALT\" /><br/>"
      + "<input type=submit /><br/>"
      + "</form>"
  		+ "</body></html>";
  private final CachedConfig cachedConfig;

  @Inject
  public AdminServlet(CachedConfig cachedConfig) {
    this.cachedConfig = cachedConfig;
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    ConfigEntry entry = cachedConfig.getConfig();
    if (entry == null) {
      entry = new ConfigEntry("", "", "", "", "");
    }
    String page = htmlPage
        .replaceAll("KEY", nullProof(entry.getConsumerKey()))
        .replaceAll("SECRET", nullProof(entry.getConsumerSecret()))
        .replaceAll("SHARED", nullProof(entry.getSharedAlbumUserId()))
        .replaceAll("SITENAME", nullProof(entry.getSiteName()))
        .replaceAll("SALT", nullProof(entry.getPostTokenSalt()));
    resp.getWriter().write(page);
  }

  String nullProof(String str) {
    if (str == null) {
      return "";
    }
    return str;
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    ConfigEntry e = new ConfigEntry(req.getParameter("key"),
        req.getParameter("secret"),
        req.getParameter("shared"),
        req.getParameter("sitename"),
        req.getParameter("salt"));
    cachedConfig.storeConfig(e);
    resp.getWriter().println("<html><body>Configuration saved!</body></html>");
  }
}
